﻿/*
 *  DFUtil - a utility for creating and implementing blueprints in Dwarf Fortress.
 *  
 *  Copyright (C) 2009  Katharine Gillis
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/.
*/

using System;
using System.Runtime.InteropServices;

namespace DFUtil
{
    /// <summary>
    /// Exposes particular Windows API functions related to windows and sending keystrokes to windows.
    /// </summary>
    sealed class WinAPI
    {
        #region Native Windows API functions

        [DllImport("user32.dll", SetLastError = true, EntryPoint="FindWindow")]
        static extern IntPtr _findWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", EntryPoint="SetForegroundWindow")]
        static extern bool _setForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint="IsWindow")]
        static extern bool _isWindow(IntPtr hwnd);

        #endregion

        #region WM_KEYDOWN message potential first parameters

        public const uint VK_RETURN = 0x0D;
        public const uint VK_SHIFT = 0x10;
        public const uint VK_SPACE = 0x20;
        public const uint VK_LEFT = 0x25;
        public const uint VK_UP = 0x26;
        public const uint VK_RIGHT = 0x27;
        public const uint VK_DOWN = 0x28;
        public const uint VK_MULTIPLY = 0x6A;
        public const uint VK_ADD = 0x6B;
        public const uint VK_SUBTRACT = 0x6D;
        public const uint VK_DIVIDE = 0x6F;

        #endregion

        #region Exposed functions

        /// <summary>
        /// Returns a pointer to a window with the given name.
        /// </summary>
        /// <param name="lpClassName">Class name of the window (as given in RegisterWindowEx).</param>
        /// <param name="lpszWindow">Title of window.</param>
        /// <returns>Handle for window.</returns>
        public static IntPtr FindWindow(string lpClassName, string lpWindowName)
        {
            return _findWindow(lpClassName, lpWindowName);
        }

        /// <summary>
        /// Checks to see if a given window handle is still in use.
        /// </summary>
        /// <param name="hWnd">Handle for window.</param>
        /// <returns>True if it is in use, false if it is not.</returns>
        public static bool IsWindow(IntPtr hWnd)
        {
            return _isWindow(hWnd);
        }

        /// <summary>
        /// Sets the given window handle as the active window.
        /// </summary>
        /// <param name="hWnd">Handle for window.</param>
        /// <returns></returns>
        public static bool SetForegroundWindow(IntPtr hWnd)
        {
            return _setForegroundWindow(hWnd);
        }

        #endregion
    }
}
